const request = {}
let domain = 'http://139.196.209.46:4882';
// domain = 'https://retail.gmcc.shop/retailApi';
 // let domain = '';
// // #ifdef H5
// domain = '/api'
// // #endif
// // #ifndef H5
// // domain = 'https://boxtest.matrixsun.cn/api'
// domain = 'http://139.196.209.46:4678'
// #endif

const SIGN_SECRECT = "12s9fds#1*&^sd";
// import dsBridge from 'dsbridge'
request.globalRequest = (url, method, data) => {
	let tempdata = data
	if (tempdata == null) {
		tempdata = {}
	}
	// tempdata['vsn'] = 100005
	// tempdata['channel'] = 'vivo'
	// #ifdef APP-PLUS
	// if (tempdata) {
	// 	Object.assign(tempdata, {
	// 		"channel": 'vivo',
	// 		"vsn": uni.getStorageSync('version'),
	// 		"client": uni.getStorageSync('client')
	// 	})
	// } else {
	// 	tempdata = {
	// 		"channel": uni.getStorageSync("channel"),
	// 		"vsn": uni.getStorageSync('version'),
	// 		"client": uni.getStorageSync('client')
	// 	}
	// }
	// #endif

	tempdata['time'] = Date.parse(new Date()) / 1000;

	// let sign = calculateSign(tempdata, SIGN_SECRECT)
	return new Promise((resolve, reject) => {
		var token = '11';//dsBridge.call("getToken");
		if (token == '' || token == undefined) {
			token = uni.getStorageSync("token") ?? ''
		}
		var domain1 = '' ;//dsBridge.call("getBaseUrl");
		console.log("getBaseUrl",domain1 )
		if (domain1 == '' || domain1 == undefined) {
			domain1 = domain
		}
		uni.request({
			url: domain1 + url,
			method,
			data: tempdata,
			dataType: 'json',
			header: {
				// 'content-type': 'application/x-www-form-urlencoded',
				'content-type': 'application/json',
				'bx_auth_ticket': token
				// 'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : "",
				// 'sign': sign
			},
			success(res) {
				// uni.showToast({
				// 	title:"请登录",
				// 	icon:"none"
				// })
				if (res.data.code == 401) {
					uni.showToast({
						title: "请登录",
						icon: "none"
					})
					uni.navigateTo({
						url: "/pages/login/index"
					})
					return false
				} else if (res.data.state != "SUCCESS") {
					uni.showToast({
						icon: "none",
						title: res.data.resultMessage
					})
				}
				resolve(res.data)
			},
			fail: (err) => {
				reject(err);
				uni.showToast({
					title: err
				})
			}
		})
	})
}
request.uploadImage = (url, filePath, imageName, data) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: domain + "/file/upload",
			filePath: filePath,
			name: imageName,
			formData: data,
			success: (res) => {
				resolve(res)
			}
		});
	})
}


request.uploadImages = ( files, data) => {
	// var token = dsBridge.call("getToken");
	if (token == '' || token == undefined) {
		token = uni.getStorageSync("token") ?? ''
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: domain + "/file/upload",
			files: files,
			formData: data,
			header: { 
				// 'content-type': 'multipart/form-data',
				'bx_auth_ticket': token
			},
			success: (res) => {
				resolve(res)
			},fail(res) {
				resolve(res)
			}
		});
	})
}

var isSignValue = function(key, value) {

	if (key == 'nickname') {
		return false;
	}

	if (value == '' || value == 'null' || value == 'NULL' || value == null || value == 'undefined' || value ==
		undefined || value.length > 50) {
		return false;
	}

	value = value + '';

	if (value.indexOf("http") == 0) {
		return false;
	}

	if (value.indexOf("{") >= 0 || value.indexOf("[") >= 0) {
		return false;
	}

	return true;
};

var sort = function(arrData) {
	let keysSorted = Object.keys(arrData).sort();
	let newD = new Array();

	for (let i = 0; i < keysSorted.length; i++) {
		newD[keysSorted[i]] = arrData[keysSorted[i]];
	}

	return newD;
};

var calculateSign = function(paramMap, secrect) {
	let signParamArr = sort(paramMap);
	var signParams = '';
	for (let itemKey in signParamArr) {
		let value = signParamArr[itemKey];

		if (!isSignValue(itemKey, value)) {
			continue;
		}

		if (signParams != '') {
			signParams += '&';
		}

		itemKey = "" + itemKey;
		value = "" + value;
		signParams += itemKey.trim() + "=" + value.trim();
	}

	signParams += secrect;
	var md5 = require("./md5.js");

	var signed = md5.md5(signParams);
	return signed;
};

export default request
